package com.business.wechat.config;

import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

/**
 * Created by FirenzesEagle on 2016/6/1 0001.
 * Email:liumingbo2008@gmail.com
 */
public class MenuConfig {

	/**
	 * 定义菜单结构
	 *
	 * @return
	 */
	protected static WxMenu getMenu() {

		//MainConfig mainConfig = new MainConfig("wx5dc785b928e23296", "0c361ff02e7dc262e4bb3fcd76240e54", "kgComc8W47EUFryu", "aesKey");
        MainConfig mainConfig = new MainConfig("wx9ff063f361283796", "ce026c40cb6fe84b54048c97e562d86f", "DTYCU0293TED", "VqKtaSQUT52x88dfkJskaJdEOrGWfpK80ZUxq2aCsc4");
        WxMpService wxMpService = mainConfig.wxMpService();

		WxMenu menu = new WxMenu();
		WxMenuButton button1 = new WxMenuButton();
		button1.setName("合作");
        WxMenuButton button11 = new WxMenuButton();
        button11.setType(MenuButtonType.VIEW);
        button11.setName("公司简介");
        button11.setUrl("http://www.baidu.com");
        WxMenuButton button12 = new WxMenuButton();
        button12.setType(MenuButtonType.VIEW);
        button12.setName("品牌故事");
        button12.setUrl("http://www.baidu.com");
        WxMenuButton button13 = new WxMenuButton();
        button13.setType(MenuButtonType.VIEW);
        button13.setName("代理商申请");
        button13.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://xudongyi.tunnel.qydev.com/weixin-platform/wechat/redirectWxUrl.do?router=guide_type:1", "snsapi_base", "1"));
        WxMenuButton button14 = new WxMenuButton();
        button14.setType(MenuButtonType.VIEW);
        button14.setName("加盟商申请");
        button14.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://xudongyi.tunnel.qydev.com/weixin-platform/wechat/redirectWxUrl.do?router=guide_type:2", "snsapi_base", "1"));
        button1.getSubButtons().add(button11);
        button1.getSubButtons().add(button12);
        button1.getSubButtons().add(button13);
        button1.getSubButtons().add(button14);
		WxMenuButton button2 = new WxMenuButton();
        button2.setName("商城");
        button2.setType(MenuButtonType.VIEW);
        button2.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://xudongyi.tunnel.qydev.com/weixin-platform/wechat/redirectWxUrl.do?router=shopmall", "snsapi_base", "1"));

        WxMenuButton button3 = new WxMenuButton();
        button3.setName("我的");
        button3.setType(MenuButtonType.VIEW);
        button3.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://xudongyi.tunnel.qydev.com/weixin-platform/wechat/redirectWxUrl.do?router=center", "snsapi_base", "1"));



        menu.getButtons().add(button1);
		menu.getButtons().add(button2);
		menu.getButtons().add(button3);

		return menu;
	}


	public static class MainConfig {

		private String appId;
		private String appSecret;
		private String token;
		private String aesKey;

		/**
		 * 为了生成自定义菜单使用的构造函数
		 *
		 * @param appId
		 * @param appSecret
		 * @param token
		 * @param aesKey
		 */
		protected MainConfig(String appId, String appSecret, String token, String aesKey) {
			this.appId = appId;
			this.appSecret = appSecret;
			this.token = token;
			this.aesKey = aesKey;
		}

		public WxMpConfigStorage wxMpConfigStorage() {
			WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
			configStorage.setAppId(this.appId);
			configStorage.setSecret(this.appSecret);
			configStorage.setToken(this.token);
			configStorage.setAesKey(this.aesKey);
			return configStorage;
		}

		public WxMpService wxMpService() {
			WxMpService wxMpService = new WxMpServiceImpl();
			wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
			return wxMpService;
		}

	}

	/**
	 * 运行此main函数即可生成公众号自定义菜单，注意要修改MainConfig构造方法行代码的对应四个参数为实际值
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		//MainConfig mainConfig = new MainConfig("wx5dc785b928e23296", "0c361ff02e7dc262e4bb3fcd76240e54", "kgComc8W47EUFryu", "aesKey");
        MainConfig mainConfig = new MainConfig("wx9ff063f361283796", "ce026c40cb6fe84b54048c97e562d86f", "DTYCU0293TED", "VqKtaSQUT52x88dfkJskaJdEOrGWfpK80ZUxq2aCsc4");
        WxMpService wxMpService = mainConfig.wxMpService();
		try {
			wxMpService.getMenuService().menuCreate(getMenu());
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
	}

}
